---@diagnostic disable: lowercase-global
local skynet = require "skynet"

local lib = require "rsa"

rsalua = rsalua or {}
local rsalua = rsalua

function rsalua.encrypt(s)
	skynet.error("rsa_encrypt:", s)
	local r = lib.encrypt(s)
	skynet.error("rsa_encrypt_r:", r)
	return r
end

function rsalua.pub_encrypt(s, pubkey)
	skynet.error("rsalua.pub_encrypt")
	skynet.error(pubkey)
	local r = "-----BEGIN PUBLIC KEY-----"
	r = r .. "\n"
	local n = #pubkey
	for i = 1, n, 64 do
		local b = i + 64;
		if b > n then
			b = n + 1
		end
		r = r .. string.sub(pubkey, i, b - 1)
		r = r .. "\n"
	end
	r = r .. "-----END PUBLIC KEY-----"
	local pubkey = r
	skynet.error(pubkey)
	local rs = lib.pub_encrypt(s, pubkey)
	return rs
end

function rsalua.decrypt(s)
	local r = lib.decrypt(s)
	return r
end

return rsalua
